//判断手机版本
//gt_ios4();


function gt_ios4() {
	// 判断是否 iPhone 或者 iPod
	if((navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i))) {
		// 判断系统版本号是否大于 4
		$("#shiping").attr({
			"webkit-playsinline": "true",
			"x-webkit-airplay": "true",
			"playsinline": "true",
			"x5-video-player-type": "h5",
			"x5-video-player-fullscreen": "true",
			"width": "100%",
			"height": "100%",
			"preload": "auto",
			"controls": "controls"
		});
	} else {
		$("#shiping").attr({
			"webkit-playsinline": "",
			"x-webkit-airplay": "",
			"playsinline": "",
			"x5-video-player-type": "",
			"x5-video-player-fullscreen": "",
			"width": "",
			"height": "",
			"preload": "",
			"controls": ""
		});
	}
}
//文件路径
var http = "http://file.ixinyun.cn/";
//获取立即购买
var buy = $("#kechengzhuti>article>.banner_1").find("a");
//获取价格
var price = $("#kechengzhuti>article>.banner_1").find("h3");
//获取视频头视频尾
var vio = [];
//播放的节次
var bofang_jieci=[];
//判断是否购买
var baofang_goumai=false;
//移除
function yichu() {
	$("#kechengzhuti>article>.banner_3>.d1>p").remove();
	$("#kechengzhuti>article>.banner_3>.d4>p").remove();
	$("#kechengzhuti>article>.banner_3>.d2>.daoshijieshao>p").remove();
}
//课程小节
var xiaojie = $("#kechengzhuti>article>.banner_3>.video>ul");
//节数
var kechengjieshu;
function video(id) {
	new Ajax("/index/kecheng").keyvalue("id", id).getJson(function(json) {
		xiaojie.empty(); //清空原有内容
		guanggao(id); //片头片尾
		$.each(json, function(k, v) {
			course(v); //写入详细信息
			xiaojie.html(jieci(v["totalnode"],2)); //写入有多少节小课 
			kechengjieshu = v["totalnode"];
			paixu(v["id"], v["status"]);
		});
	});
}

function guanggao(id) {
	new Ajax("/index/kechengshiping").keyvalue("type", "3").keyvalue("kechengid", id).getJson(function(jso) {
		$.each(jso, function(key, va) {
			if(va.type == "1") {
				vio[0] = va.url;
			} else {
				vio[2] = va.url;
			}
		})
	})
}

var kcpayhf = "/fenxiang/paytransist.html?kechengid="
//点击进入后详情
function course(v) {
	$("#sharetle").text(v["title"]);
	$("#kechengbuybtn").attr("href", kcpayhf + "" + v["id"] + "&tit=" + v["title"] + "&jiage=" + v["jiage"] + "")
	$("#kechengzhuti>article>.banner>video").attr("src", "");
	$("#kechengbuybtn").attr("kechengid", v["id"]);
	$("#kechengzhuti>article").attr("dataid", v["id"]); //此处id为每套视频的id
	$("#kechengzhuti>article>.banner>video").attr("src", http + v["freeurl"]);
	$("#kechengzhuti>article>.banner>video").attr("poster", http + v["ico"]);
	$("#kechengzhuti>article>.banner_1>h1").text(v["title"]);
	$("#kechengzhuti>article>.banner_1>h3>span").text(v["jiage"]);
	$("#kechengzhuti>article>.banner_1>h4>span").text(v["canyurenshu"]);
	$("#kechengzhuti>article>.banner_3>ul>li:first-of-type>h1").append(v["jieshao"]);
	$("#kechengzhuti>article>.banner_3>.d4").append(v["shixiang"]);
	$("#kechengzhuti>article>.banner_3>ul>li:nth-of-type(3)>ol").text(v["renqun"]); //适应人群
	$("#kechengzhuti>article>.banner_3>ul>li:nth-of-type(2)>h1>img:first-of-type").attr("src", http + v["teacher_ico"]);
	$("#kechengzhuti>article>.banner_3>ul>li:nth-of-type(2)>h1>b").text(v["teachername"]); //导师名字
	$("#kechengzhuti>article>.banner_3>ul>li:nth-of-type(2)>h1>p").append(v["teacher_description"]);

	var data = {
		title: v["title"], // 分享标题
		desc: v["jieshao"], // 分享描述
		link: location.href, // 分享链接
		imgUrl: http + v["ico"], // 分享图标
		type: 'link', // 分享类型
		success: function() {
			// 用户确认分享后执行的回调函数
		},
		cancel: function() {
			// 用户取消分享后执行的回调函数
		}
	}
	wx.onMenuShareAppMessage(data);

	new Ajax("/buycourse").keyvalue("kechengid", v["id"]).get(function(data) {
		if(data == "1") {
			buy.text("已购买");
			$.getScript("fenxiang/xianshang.js");
			buy.attr("href", "javascript:void(0);")
		} else {
			buy.text("立即购买");
			price.show();
		}
	})
}
//默认将所有节次课程设置为敬请期待
function jieci(jieshu,mon) {
	var txt = "";
	var wenzi = "";
	if(mon==1){
		wenzi="付费观看";
	}else{
		wenzi="敬请期待";
	}
	
	for(var i = 1; i <= jieshu; i++) {
		txt += "<li><h1>第" + i + "节</h1><h2>"+wenzi+"</h2><span>00:00</span></li>";
	}
	return txt;
};
//对于小节课程进行排序
function paixu(kechengid, mianfei) {

	//如果没有购买视频播放结束后弹窗
	new Ajax("/index/xianshangdingdan").keyvalue("kechengid", kechengid).get(function(jso) {
		if(jso != "1" && mianfei == "1") {
			$("#kechengzhuti>article>.banner>video").on("ended", function() {
				alert("购买该视频后即可观看更多精彩内容");
			})
		} 
		    var paixu = [];
			new Ajax("/index/kechengshiping").keyvalue("kechengid", kechengid).keyvalue("type", "1").getJson(function(jso) {
				xiaojie.html(jieci(kechengjieshu,2));
				$.each(jso, function(key, val) {
					paixu.push(val);
				})
				JSON.stringify(paixu.sort(function(x, y) {
					var x = x["jieci"],
						y = y["jieci"];
					if(x == y) {
						return 0;
					}
					return x - y;
				}));
				var jie = $("#kechengzhuti>article>.banner_3>.video>ul").find("li");
				for(var i = 0; i < paixu.length; i++) {
					i = parseInt(i);
					var j = 1 + i;
					var txt = "";
					var shijian = parseInt(paixu[i].shichang) + 28800;
					shijian = Math.floor(shijian / 60) + ':' + (shijian - (Math.floor(shijian / 60)) * 60);
					if(shijian.length == 4) {
						shijian = shijian.replace(":", ":0");
					}
					txt = "<h1 jieci="+i+" sr='" + paixu[i].id + "," + paixu[i].kechengid + "'>第" + j + "节</h1><h2>" + paixu[i].title + "</h2><span>" + shijian + "</span>";
					jie.eq(i).empty();
					jie.eq(i).html(txt);
				}
				boder(kechengid);
				//点击小节
				jie.click(function() {
					//获取点击的元素
					var jieci = $(this);
					//调取个人id验证该用户已近购买该课程执行以下方法
					new Ajax("/index/xianshangdingdan").keyvalue("kechengid", kechengid).get(function(jso) {
						if(jso != "1" && mianfei == "1") {
							alert("请先购买该视频");
						} else {
							if(jieci.find("h2").text() != "敬请期待") {
								var i = "",
									k = "",
									j = 0;
								var str = jieci.find("h1").attr("sr");
								var arr = [];
								arr = str.split(",");
								i = arr[0], k = arr[1];
								new Ajax("/index/kechengshiping").keyvalue("id", i).keyvalue("kechengid", kechengid).keyvalue("type", "2").getJson(function(jso) {
									vio[1] = jso[0].url;
									$("#kechengzhuti>article>.banner>video").attr("src", http + vio[0]);
									$("#kechengzhuti>article>.banner>video")[0].play();
									$("#kechengzhuti>article>.banner>video").on("ended", function() {
										j++;
										if(j == "3") {
										    var num =  jieci.find("h1").attr("jieci")*1+1;
											if(baofang_goumai){
												new Ajax("/video_record").keyvalue("myid",RssUser.Data.myid).keyvalue("kechengid",k).keyvalue("sectionid",num).get();
											}else{
												var jiance =0;
												$.each(bofang_jieci, function(k,y) {
													if(y==num){
														jiance =1;
													}
													
												});
												if(!jiance){
													new Ajax("/video_record").keyvalue("myid",RssUser.Data.myid).keyvalue("kechengid",k).keyvalue("sectionid",num).get();
												}
											}
										 $("#kechengzhuti>article>.banner_3>.video>ul").find("li").eq(jieci.find("h1").attr("jieci")).css('box-shadow','0 0 0.1rem #888a8b');
											alert("本节视频已播放结束");
											return;
										} else {
											$("#kechengzhuti>article>.banner>video")[0].click();
											$("#kechengzhuti>article>.banner>video").attr("src", http + vio[j]);
											$("#kechengzhuti>article>.banner>video")[0].play();
										}
									});
								})
								$(this).siblings().removeClass("video_1");
								$(this).addClass("video_1");
							}
						}
					})
				})
			});
	})

};
//当课程播放结束以后改变其边框的颜色
function boder(ke){
	new Ajax("/video_records").keyvalue("myid",RssUser.Data.myid).keyvalue("kechengid",ke).getJson(function(json){
		if(json==2){
			baofang_goumai=false;
		}else{
			bofang_jieci=[];
			$.each(json, function(k,y) {
				bofang_jieci.push(y.sectionid);
				$("#kechengzhuti>article>.banner_3>.video>ul").find("li").eq(y.sectionid*1-1).css('box-shadow','0 0 0.1rem #888a8b');
			});
			baofang_goumai=true;
			
		}
	})
	
}
